﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectRoughWork
{
    class ProcessManager
    {
        /// <summary>
        /// Default Constructor. Initializes the list of CProcesses
        /// </summary>

        public ProcessManager()
        {
            attachedProcesses = new List<CProcess>();
        }

        /// <summary>
        /// Attaches a new process to the Process Manager
        /// </summary>
        /// <param name="newProcess">The new process to be attached</param>

        public void Attach(CProcess newProcess)
        {
            attachedProcesses.Add(newProcess);
        }

        /// <summary>
        /// Removes the given process from the Process Manager
        /// </summary>
        /// <param name="targetProcess">The process to be removed</param>

        public void Detach(CProcess targetProcess)
        {
            attachedProcesses.Remove(targetProcess);
        }

        /// <summary>
        /// Updates all processes attached o the process manager
        /// </summary>

        public void Run()
        {
            //Loops through all attaches processes

            for (int index = attachedProcesses.Count - 1; index >= 0; index--)
            {
                CProcess currentProcess = attachedProcesses[index];

                //If the process is active, update it

                if (currentProcess.isActive())
                {
                    currentProcess.VOnUpdate();
                }

                //If the process is dead, call it's kill function and detach it. 

                if (currentProcess.isDead())
                {
                    currentProcess.VKill();
                    Detach(currentProcess);
                }
            }
        }


        //Data members

        private List<CProcess> attachedProcesses;
    }
}
